//
//  MBCompetationContentCell.m
//  MiBaoTu
//
//  Created by admin on 16/11/11.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBCompetationContentCell.h"

@interface MBCompetationContentCell() {
    UIView              *_bgView;
    UILabel             *_numLb;
    UIImageView         *_headImgView;
    UILabel             *_babyNameLb;
    UIImageView         *_timeImgView;
    UILabel             *_timeNameLb;
    UIImageView         *_listenImgView;
    UILabel             *_listenNameLb;
    UIImageView         *_zanImgView;
    UILabel             *_zanNameLb;
    UIView              *_lineView;
}

@end
@implementation MBCompetationContentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_bgView];
    
    //序号
    _numLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _numLb.backgroundColor = [UIColor clearColor];
    _numLb.textAlignment = NSTextAlignmentCenter;
    _numLb.textColor = UIColorFromHex(0x333333);
    _numLb.font = [UIFont boldSystemFontOfSize:15.f];
    [_bgView addSubview:_numLb];
    CGAffineTransform transform = CGAffineTransformMake(1, 0, tanf(-15 * (CGFloat)M_PI / 180), 1, 0, 0);
    _numLb.transform = transform;
    
    //头像
    _headImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _headImgView.backgroundColor = [UIColor clearColor];
    _headImgView.layer.cornerRadius = 5.;
    _headImgView.layer.masksToBounds = YES;
    [_bgView addSubview:_headImgView];
    
    //名字
    _babyNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _babyNameLb.textAlignment = NSTextAlignmentLeft;
    _babyNameLb.textColor = UIColorFromHex(0x333333);
    _babyNameLb.font = [UIFont systemFontOfSize:14.f];
    [_bgView addSubview:_babyNameLb];
    
    //时间
    _timeImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _timeImgView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_timeImgView];
    
    _timeNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeNameLb.textAlignment = NSTextAlignmentLeft;
    _timeNameLb.textColor = UIColorFromHex(0x999999);
    _timeNameLb.font = [UIFont systemFontOfSize:11.f];
    [_bgView addSubview:_timeNameLb];
    
    //试听
    _listenImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _listenImgView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_listenImgView];
    
    _listenNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _listenNameLb.textAlignment = NSTextAlignmentLeft;
    _listenNameLb.textColor = UIColorFromHex(0x999999);
    _listenNameLb.font = [UIFont systemFontOfSize:11.f];
    [_bgView addSubview:_listenNameLb];
    
    //试听
    _zanImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _zanImgView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_zanImgView];
    
    _zanNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _zanNameLb.textAlignment = NSTextAlignmentLeft;
    _zanNameLb.textColor = UIColorFromHex(0x999999);
    _zanNameLb.font = [UIFont systemFontOfSize:11.f];
    [_bgView addSubview:_zanNameLb];
    
    if (ISIPAD) {
        _babyNameLb.font = [UIFont systemFontOfSize:30*PERCEN];
         _timeNameLb.font = [UIFont systemFontOfSize:26*PERCEN];
         _listenNameLb.font = [UIFont systemFontOfSize:26*PERCEN];
         _zanNameLb.font = [UIFont systemFontOfSize:26*PERCEN];
    }
    //分割线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0xcccccc)colorWithAlphaComponent:.45] ;
    [_bgView addSubview:_lineView];
}

- (void)setIdString:(NSString *)idString
{
    _idString = idString;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (!_dataDic)return;
    
    //value
    _numLb.text = [NSString stringWithFormat:@"%d",[_idString intValue]+1];
    if (_idString) {
        if ([_idString intValue] == 0) {
            _numLb.textColor = UIColorFromHex(0xe95078);
        }
        else if ([_idString intValue] == 1) {
            _numLb.textColor = UIColorFromHex(0xeb6fa7);
        }
        else if ([_idString intValue] == 2) {
            _numLb.textColor = UIColorFromHex(0xf7c658);
        }
        else if ([_idString intValue] >= 3) {
            _numLb.textColor = UIColorFromHex(0x333333);
        }
    }
    NSString *st = [_dataDic objectForKey:@"FaceImg"];
    if ([st isKindOfClass:[NSNull class]]) {
        _headImgView.image = [UIImage imageNamed:@"activity_bg"];
    } else {
        [_headImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,st]] placeholderImage:[UIImage imageNamed:@"activity_bg"]];
    }
    
    _babyNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];
    
    NSString *interval = [_dataDic objectForKey:@"CreateTime"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[interval doubleValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    _timeNameLb.text = [dateFormatter stringFromDate:date];
    
    _listenNameLb.text = [NSString stringWithFormat:@"试听%@次",[[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"AuditionNum"]] getValues]];
    
    _zanNameLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"PraiseNum"]] getValues];
    
    UIImage *timeImg = [UIImage imageNamed:@"shijian_qzxbf"];
    _timeImgView.image = timeImg;
    UIImage *listenImg = [UIImage imageNamed:@"shiting_qzxbf"];
    _listenImgView.image = listenImg;
    UIImage *zanImg = [UIImage imageNamed:@"zan1"];
    _zanImgView.image = zanImg;
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 144*PERCEN);
    if (ISIPAD) {
         _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 180*PERCEN);
    }
    _numLb.frame = CGRectMake(5, 0, 64*PERCEN-5, _bgView.height);
    if (ISIPAD) {
         _numLb.frame = CGRectMake(460*PERCEN, 0, 64*PERCEN, _bgView.height);
    }
    _headImgView.frame = CGRectMake(_numLb.right, (_bgView.height-106*PERCEN)/2., 106*PERCEN, 106*PERCEN);
    if (ISIPAD) {
          _headImgView.frame = CGRectMake(_numLb.right+30*PERCEN, 20*PERCEN , 140*PERCEN, 140*PERCEN);
    }
    if (ISIPAD) {
         _babyNameLb.frame = CGRectMake(_headImgView.right+20*PERCEN, 20*PERCEN, 400*PERCEN, 30*PERCEN);
        _timeImgView.frame = CGRectMake(_babyNameLb.left, _babyNameLb.bottom+40*PERCEN, timeImg.size.width, timeImg.size.height);
        _timeNameLb.frame = CGRectMake(_timeImgView.right+10*PERCEN, _timeImgView.top, 400*PERCEN, 26*PERCEN);
        _listenImgView.frame = CGRectMake(_babyNameLb.left, _bgView.height-20*PERCEN-listenImg.size.height, listenImg.size.width, listenImg.size.height);
         _listenNameLb.frame = CGRectMake(_listenImgView.right+10*PERCEN, _listenImgView.top, 400*PERCEN, 26*PERCEN);
        
        float zanWidth = [_zanNameLb.text boundingRectWithSize:CGSizeMake(1000, 26*PERCEN) options:1|2 attributes:@{NSFontAttributeName:_zanNameLb.font} context:nil].size.width;
        _zanNameLb.frame = CGRectMake(_bgView.width-zanWidth-460*PERCEN, _listenNameLb.top, zanWidth, 26*PERCEN);
        _zanImgView.frame = CGRectMake(_zanNameLb.left-zanImg.size.width-10*PERCEN, _zanNameLb.top, zanImg.size.width, zanImg.size.height);

    }else {
    float juHeight = (_bgView.height-60)/4.;
    _babyNameLb.frame = CGRectMake(_headImgView.right+18*PERCEN, juHeight+3, 200, 20);
    
    _timeImgView.frame = CGRectMake(_babyNameLb.left, _babyNameLb.bottom+juHeight-(timeImg.size.height-20)/2., timeImg.size.width, timeImg.size.height);
    _timeNameLb.frame = CGRectMake(_timeImgView.right+10*PERCEN, _babyNameLb.bottom+juHeight, 200, _babyNameLb.height);
    
    _listenImgView.frame = CGRectMake(_babyNameLb.left, _timeNameLb.bottom+juHeight-(listenImg.size.height-20)/2-3., listenImg.size.width, listenImg.size.height);
    _listenNameLb.frame = CGRectMake(_listenImgView.right+10*PERCEN, _timeNameLb.bottom+juHeight-3, 200, _babyNameLb.height);
    
    float zanWidth = [_zanNameLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:1|2 attributes:@{NSFontAttributeName:_zanNameLb.font} context:nil].size.width;
    _zanNameLb.frame = CGRectMake(_bgView.width-zanWidth-24*PERCEN, _listenNameLb.top, zanWidth, _babyNameLb.height);
    _zanImgView.frame = CGRectMake(_zanNameLb.left-zanImg.size.width*.9-10*PERCEN, _zanNameLb.top-(zanImg.size.height*.9-20)/2., zanImg.size.width*.9, zanImg.size.height*.9);
    }
    _lineView.frame = CGRectMake(0, _bgView.height-1, _bgView.width, 1);
    if (ISIPAD) {
        _lineView.frame = CGRectMake(440*PERCEN, _bgView.height-1, _bgView.width-2*440*PERCEN, 1);
    }
}

@end
